//
//  CategoryViewController.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryCollectionViewCell.h"
#import "CategoryDetialVC.h"
#import "SearchViewController.h"

@interface CategoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL isPush;
@end


static NSString *categoryCell = @"categoryCell";
@implementation CategoryViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.isPush = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.isPush) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"分类";
    self.view.backgroundColor = KLColor_C1;
    [self.view addSubview:self.collectionView];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"icon_sousuo"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
}

- (void)search{
    self.isPush = YES;
    SearchViewController *searchVc = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:searchVc animated:YES];
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        [layout setMinimumLineSpacing:15*HEIGHT];
        [layout setMinimumInteritemSpacing:0*WIDTH];
        [layout setItemSize:CGSizeMake(165*WIDTH, 80*HEIGHT)];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15*WIDTH, 20*HEIGHT, 345*WIDTH,KLScreenH-NaviH - TabbarH-20*HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = KLColor_C1;
        [_collectionView registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:categoryCell];
        //注册头视图
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = YES;
    }
    return _collectionView;
}



#pragma mark -- UICollectionViewDataSource
/** 每组cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 15;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CategoryCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}




#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第 %zd组 第%zd个",indexPath.section, indexPath.row);

    CategoryDetialVC *detialVc = [[CategoryDetialVC alloc]init];
    [self.navigationController pushViewController:detialVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
